﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WarCardGame
{
    public partial class WarGame : System.Web.UI.Page
    {
        private List<Card> player1Cards = new List<Card>();
        private List<Card> player2Cards = new List<Card>();
        private List<Card> player1Move = new List<Card>();
        private List<Card> player2Move = new List<Card>();
        private bool gameWinStatus = false;


        protected void Page_Load(object sender, EventArgs e)
        {
            //get player's names from session
            lblPlayer1Name.Text = (String)Session["Player1Name"];
            lblPlayer2Name.Text = (String)Session["Player2Name"];

            if (!IsPostBack)
            {
                var deckOfCards = CreateDeck();
                ShuffleDeck(deckOfCards);
                DistributeCards(deckOfCards);
                UpdateCardCount();
                btnEvaluate.Enabled = false;
            }
            else
            {
                player1Cards = (List<Card>)Session["player1Cards"];
                player2Cards = (List<Card>)Session["player2Cards"];
                player1Move = (List<Card>)Session["player1Move"];
                player2Move = (List<Card>)Session["player2Move"];
            }
        }

        //update no of cards display
        protected void UpdateCardCount()
        {
            lblp1TotalCard.Text = Convert.ToString(player1Cards.Count);
            lblp2TotalCard.Text = Convert.ToString(player2Cards.Count);
        }

        protected void Page_Unload(object sender, EventArgs e)
        {
            Session["player1Cards"] = player1Cards;
            Session["player2Cards"] = player2Cards;
            Session["player1Move"] = player1Move;
            Session["player2Move"] = player2Move;
        }

        private List<Card> CreateDeck()
        {
            var suits = new List<String>
                            {
                                "Hearts",
                                "Clubs",
                                "Diamonds",
                                "Spades"
                            };

            var deckOfCards = new List<Card>();

            foreach (var suit in suits)
            {
                for (var i = 1; i < 14; i++)
                {
                    var card = new Card { Face = suit + i, Value = i == 1 ? 14 : i };
                    deckOfCards.Add(card);
                }
            }
            return deckOfCards;
        }

        private void ShuffleDeck(List<Card> deckOfCards)
        {
            var random = new Random();
            for (int i = 1; i < deckOfCards.Count; i++)
            {
                int pos = random.Next(i + 1);
                var x = deckOfCards[i];
                deckOfCards[i] = deckOfCards[pos];
                deckOfCards[pos] = x;
            }
        }

        
        private void DistributeCards(List<Card> deckOfCards)
        {
           int halfCount = deckOfCards.Count / 2;
            //get the first 1/2 of the deck for the player 1
            player1Cards = deckOfCards.GetRange(0, halfCount);

            //get the second 1/2 of the deck for the player 2
            player2Cards = deckOfCards.GetRange(halfCount, halfCount);            
        }

        //Chack each player has enough cards to play
        private bool CheckForValidNoOfCards(int checkNo, int cardCount)
        {
            if (cardCount > checkNo)
                return true;
            else
                return false;
        }

        protected void btnp1Deal_Click(object sender, EventArgs e)
        {
            lblStatus.Text = string.Empty;
            ltrPlayer1Hand.Text = string.Empty;
            ltrPlayer2Hand.Text = string.Empty;
            DrawCards(player1Cards.First().Face,1);
            player1Move.Add(player1Cards.First());
            player1Cards.Remove(player1Cards.First());
            UpdateCardCount();
            btnp1Deal.Enabled = false;
                
        }

        //draw card images
        private void DrawCards(string cardFace, int player)
        {
            if(player == 1)
                ltrPlayer1Hand.Text = ltrPlayer1Hand.Text + "<img src=Images\\" + cardFace + ".png />" ;
            else if (player == 2)
                ltrPlayer2Hand.Text = ltrPlayer2Hand.Text + "<img src=Images\\" + cardFace + ".png />";
        }

        protected void btnp2Deal_Click(object sender, EventArgs e)
        {
            DrawCards(player2Cards.First().Face, 2);
            player2Move.Add(player2Cards.First());
            player2Cards.Remove(player2Cards.First());
            UpdateCardCount();
            btnp2Deal.Enabled = false;
            btnEvaluate.Enabled = true;
        }


        protected void btnEvaluateHand_Click(object sender, EventArgs e)
        {
            btnEvaluate.Enabled = false;
            if (player1Move.Count > 0 && player2Move.Count > 0)
            {
                lblStatus.Text = GetResult();
                UpdateCardCount();

                if (!CheckForValidNoOfCards(0, player1Cards.Count)) 
                {
                    lblStatus.ForeColor = System.Drawing.Color.Red;
                    lblStatus.Text = "Player 2 Won the Game... ";
                    gameWinStatus = true;
                }
                else if(!CheckForValidNoOfCards(0, player2Cards.Count))
                {
                    lblStatus.ForeColor = System.Drawing.Color.Red;
                    lblStatus.Text = "Player 1 Won the Game... ";
                    gameWinStatus = true;
                }
                else
                {
                    player1Move.Clear();
                    player2Move.Clear();                    
                }
                if (!gameWinStatus)
                {
                    btnp1Deal.Enabled = true;
                    btnp2Deal.Enabled = true;
                }
            }
            else
            {
                lblStatus.Text = "Slow Down, Both Players need to deal";
                btnEvaluate.Enabled = true;
            }
        }

        // get the results for each card hand
        private string GetResult()
        {
            string result = "";

            if (player1Move.Last().Value == player2Move.Last().Value)
            {
                lblStatus.Text = "Tie! Drawing next hand... 3 cards from each player.";
                result = EvaluateTieHand();
            }
            else
            {
                result = EvaluateNormalHand();
            }
            return result;
        }

        //result for a normal hand
        protected string EvaluateNormalHand()
        {
            string result = "";

            if (player1Move.Last().Value > player2Move.Last().Value)
            {
                result = "Player 1 won the hand!";
                player1Cards.Add(player1Move.Last());
                player1Cards.Add(player2Move.Last());
            }
            else if (player2Move.Last().Value > player1Move.Last().Value)
            {
                result = "Player 2 won the hand!";
                player2Cards.AddRange(player1Move);
                player2Cards.AddRange(player2Move);
            }            
            return result;
        }

        //result for a tie hand
        protected string EvaluateTieHand()
        {
            string result = "";
            if(!CheckForValidNoOfCards(2,player1Cards.Count))
            {
                lblStatus.ForeColor = System.Drawing.Color.Red;
                result  = "Player 2 Won the Game... ";
                gameWinStatus = true;
            }
            else if (!CheckForValidNoOfCards(2,player2Cards.Count))
            {
                lblStatus.ForeColor = System.Drawing.Color.Red;
                result  = "Player 1 Won the Game... ";
                gameWinStatus = true;
            }
            else
            {
                for (int i = 0; i < 3; i++)
                {
                    DrawCards(i == 2 ? player1Cards.First().Face : "front", 1);
                    player1Move.Add(player1Cards.First());
                    player1Cards.Remove(player1Cards.First());
                    DrawCards(i == 2 ? player2Cards.First().Face : "front", 2);
                    player2Move.Add(player2Cards.First());
                    player2Cards.Remove(player2Cards.First());
                    
                }
                UpdateCardCount();
                result = GetResult();
            }
            return result;
        }

        
    }
}